package com.winway.sixbook.ui;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.winway.sixbook.HomeActivity;
import com.winway.sixbook.R;
import com.winway.sixbook.custom.CloseActivityClass;
import com.winway.sixbook.service.CounterDataManager;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.TextView;

public class CounterActivity extends Activity {
	
	private static final String TAG = "CounterActivity";

	private int old=0;
	private String id;
	private TextView t;
	private TextView todaynum;
	private CounterDataManager counterDataManager;
	private int todayold=0;
	private String oldtimeString="";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.counter);
		CloseActivityClass.activityList.add(this);
		
		t=(TextView)findViewById(R.id.countnum);
		if(counterDataManager==null){
			counterDataManager= new CounterDataManager(this);
			counterDataManager.openDataBase();
		}
		
		Intent intent1 = getIntent();
		id=intent1.getStringExtra("id");
		t=(TextView)findViewById(R.id.countnum);
		todaynum = (TextView)findViewById(R.id.todaynum);
		
		if(id!=null){
			Cursor cursor =counterDataManager.fetchCounterData(Integer.valueOf(id));
			if(cursor.getColumnCount()>0){
				cursor.moveToFirst();
				//old=cursor.getInt(cursor.getColumnIndex("countnum"));
				old=cursor.getInt(cursor.getColumnIndex("countnum"));
				todayold=cursor.getInt(cursor.getColumnIndex("counttoday"));
				oldtimeString = cursor.getString(cursor.getColumnIndex("add_time"));
				Log.v(TAG, "begin:"+old+"dd======="+cursor.getColumnIndex("countnum"));
			}
		}
		t.setText(old+"");
		todaynum.setText(todayold+"");
		
		SimpleDateFormat dateformat1=new SimpleDateFormat("yyyy-MM-dd");
    	String addtime = dateformat1.format(new Date());
    	if(null == oldtimeString ){
    		counterDataManager.updateTodayData(Integer.valueOf(id), 0,addtime);
    	}else{
    		if(!oldtimeString.equals(addtime)){
    			counterDataManager.updateTodayData(Integer.valueOf(id), 0,addtime);
    		}
    	}
	}
	@Override
	public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
	        //触摸屏幕时刻
	        case MotionEvent.ACTION_DOWN:
	        	if(counterDataManager==null){
					counterDataManager= new CounterDataManager(this);
					counterDataManager.openDataBase();
				}
	        	Integer old=Integer.valueOf(t.getText().toString())+1;
	        	//今日数据
	        	Integer today_num=Integer.valueOf(todaynum.getText().toString())+1;
	        	
				t.setText(old.toString());
				todaynum.setText(today_num.toString());
				counterDataManager.updateCounterData(Integer.valueOf(id), old,today_num);
   	     }
        
        return super.onTouchEvent(event); 
    } 

	@Override
	protected void onResume() {
		if (counterDataManager == null) {
			counterDataManager = new CounterDataManager(this);
			counterDataManager.openDataBase();
        }
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		 super.onDestroy();
		 if (counterDataManager  != null) {  
			 counterDataManager.closeDataBase(); 
			 counterDataManager = null;
		 } 
	}

	@Override
	protected void onPause() {
		if (counterDataManager != null) {
			counterDataManager.closeDataBase();
			counterDataManager = null;
        }
		super.onPause();
	}
	/**
	 * 捕捉back
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Intent counter1 = new Intent(CounterActivity.this, CounterListActivity.class);
			startActivity(counter1);
			finish();
			return true;
		}
		
		return super.onKeyDown(keyCode, event);
	}

}
